package cn.bj.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.bj.beans.Admin;
import cn.bj.beans.Advertisement_picture;
import cn.bj.beans.Rotate;
import cn.bj.beans.User;
import cn.bj.beans.Yxfw_candy;
import cn.bj.beans.Yxfw_candy_rotate;
import cn.bj.beans.Yxfw_car;
import cn.bj.beans.Yxfw_car_rotate;
import cn.bj.beans.Yxfw_jewellery;
import cn.bj.beans.Yxfw_jewellery_rotate;
import cn.bj.beans.Yxfw_man;
import cn.bj.beans.Yxfw_man_rotate;
import cn.bj.beans.Yxfw_travel;
import cn.bj.beans.Yxfw_travel_rotate;
import cn.bj.beans.Yxfw_wine;
import cn.bj.beans.Yxfw_wine_rotate;
import cn.bj.beans.Yxhq;
import cn.bj.beans.Yxhq_work_picture;
import cn.bj.beans.Yxhz;
import cn.bj.beans.Yxhz_work_picture;
import cn.bj.beans.Yxjd;
import cn.bj.beans.Yxjd_rotate;
import cn.bj.beans.Yxsx;
import cn.bj.beans.Yxsy;
import cn.bj.beans.Yxsy_work_picture;
import cn.bj.beans.Yxzc;
import cn.bj.beans.Yxzc_work_picture;
import cn.bj.service.AdminService;
import cn.bj.service.AdvertisementService;
import cn.bj.service.CandyRotateService;
import cn.bj.service.CandyService;
import cn.bj.service.CarRotateService;
import cn.bj.service.CarService;
import cn.bj.service.JewelleryRotateService;
import cn.bj.service.JewelleryService;
import cn.bj.service.ManRotateService;
import cn.bj.service.ManService;
import cn.bj.service.RotateService;
import cn.bj.service.TravelRotateService;
import cn.bj.service.TravelService;
import cn.bj.service.Userservice;
import cn.bj.service.WineRotateService;
import cn.bj.service.WineService;
import cn.bj.service.YxhqService;
import cn.bj.service.YxhqWorkPictureService;
import cn.bj.service.YxhzService;
import cn.bj.service.YxhzWorkPictureService;
import cn.bj.service.YxjdRotateService;
import cn.bj.service.YxjdService;
import cn.bj.service.YxsxService;
import cn.bj.service.YxsyService;
import cn.bj.service.YxsyWorkPictureService;
import cn.bj.service.YxzcService;
import cn.bj.service.YxzcWorkPictureService;

/*
 * 管理员控制层
 * 
 * */

@Controller
@RequestMapping("/admin")
public class AdminController {
	@Autowired
	AdminService adminService;

	@Autowired
	RotateService rotateService;

	@Autowired
	YxhqService yxhqService;

	@Autowired
	YxhqWorkPictureService yxhqWorkPictureService;

	@Autowired
	AdvertisementService advertisementSservice;

	@Autowired
	YxjdService yxjdService;

	@Autowired
	YxjdRotateService yxjdRotateService;

	@Autowired
	YxzcService yxzcService;

	@Autowired
	YxzcWorkPictureService yxzcWorkPictureService;

	@Autowired
	YxsxService yxsxService;

	@Autowired
	YxhzService yxhzService;

	@Autowired
	YxhzWorkPictureService yxhzWorkPictureService;

	@Autowired
	YxsyService yxsyService;

	@Autowired
	YxsyWorkPictureService yxsyWorkPictureService;

	@Autowired
	CandyService candyService;

	@Autowired
	CandyRotateService candyRotateService;

	@Autowired
	CarService carService;

	@Autowired
	CarRotateService carRotateService;

	@Autowired
	ManService manService;

	@Autowired
	ManRotateService manRotateService;

	@Autowired
	JewelleryService jewelleryService;

	@Autowired
	JewelleryRotateService jewelleryRotateService;

	@Autowired
	TravelService travelService;

	@Autowired
	TravelRotateService travelRotateService;

	@Autowired
	WineService wineService;

	@Autowired
	WineRotateService wineRotateService;

	@Autowired
	Userservice userService;

	@RequestMapping("/login.do")
	public String toLogin() {
		return "admin/login";
	}

	// 登陆请求
	@PostMapping("/tologin.do")
	public String toLogin(Admin admin, HttpSession session) {
		// 请求service层
		admin = adminService.validate(admin.getUsername(), admin.getPassword());
		if (null != admin) {
			session.setAttribute("admin", admin);
			// 把很多请求放在初次请求中1、放入管理员
			List<Admin> admins = adminService.getAllAdmins(null);
			session.setAttribute("admins", admins);

			// 2放入首页轮播图
			List<Rotate> rotates = rotateService.getAllRotats();
			session.setAttribute("rotates", rotates);

			// 3放入优选婚庆
			List<Yxhq> allYxhqs = yxhqService.getAllYxhq();
			session.setAttribute("allYxhqs", allYxhqs);

			// 4放入优选婚庆工作集
			List<Yxhq_work_picture> yxhqWPs = yxhqWorkPictureService.getAllYxhqWorkPicture();
			session.setAttribute("yxhqWPs", yxhqWPs);

			// 5.放入广告图
			List<Advertisement_picture> advers = advertisementSservice.selectAllAdvers();
			session.setAttribute("advers", advers);

			// 6.放入优选酒店
			List<Yxjd> yxjds = yxjdService.selectAllYxjd();
			session.setAttribute("yxjds", yxjds);

			// 7.放入酒店的轮播图
			List<Yxjd_rotate> yxjdRs = yxjdRotateService.selectAllYxjdRotate();
			session.setAttribute("yxjdRs", yxjdRs);

			// 8.放入主持人
			List<Yxzc> yxzcs = yxzcService.selcectAllYxzc();
			session.setAttribute("yxzcs", yxzcs);

			// 9.放入主持人作品集
			List<Yxzc_work_picture> yxzcWPs = yxzcWorkPictureService.getAllYxhqWorkPicture();
			session.setAttribute("yxzcWPs", yxzcWPs);

			// 10.放入优选摄像
			List<Yxsx> yxsxs = yxsxService.getAllYxhqWorkPicture();
			session.setAttribute("yxsxs", yxsxs);

			// 11.放入优选化妆
			List<Yxhz> yxhzs = yxhzService.getAllYxhz();
			session.setAttribute("yxhzs", yxhzs);

			// 12.放入化妆师作品集
			List<Yxhz_work_picture> yxhzWPs = yxhzWorkPictureService.getAllYxhzWorkPicture();
			session.setAttribute("yxhzWPs", yxhzWPs);

			// 13.放入优选化妆
			List<Yxsy> yxsys = yxsyService.getAllYxhz();
			session.setAttribute("yxsys", yxsys);

			// 14.放入化妆师作品集
			List<Yxsy_work_picture> yxsyWPs = yxsyWorkPictureService.getAllYxsyWorkPicture();
			session.setAttribute("yxsyWPs", yxsyWPs);

			// 15.放入优选化妆
			List<Yxfw_candy> candys = candyService.getAllCandys();
			session.setAttribute("candys", candys);

			// 16.放入酒店的轮播图
			List<Yxfw_candy_rotate> candyRs = candyRotateService.selectAllCandyRotate();
			session.setAttribute("candyRs", candyRs);

			// 17.放入优选化妆
			List<Yxfw_car> cars = carService.getAllCandys();
			session.setAttribute("cars", cars);

			// 18.放入酒店的轮播图
			List<Yxfw_car_rotate> carRs = carRotateService.selectAllCarRotate();
			session.setAttribute("carRs", carRs);

			// 19.放入优选化妆
			List<Yxfw_man> mans = manService.getAllCandys();
			session.setAttribute("mans", mans);

			// 20.放入酒店的轮播图
			List<Yxfw_man_rotate> manRs = manRotateService.selectAllCarRotate();
			session.setAttribute("manRs", manRs);

			// 21.放入优选珠宝
			List<Yxfw_jewellery> jewes = jewelleryService.getAllCandys();
			session.setAttribute("jewes", jewes);

			// 22.放入珠宝轮播图
			List<Yxfw_jewellery_rotate> jeweRs = jewelleryRotateService.selectAllJeweRotate();
			session.setAttribute("jeweRs", jeweRs);

			// 23.放入优选旅行
			List<Yxfw_travel> travels = travelService.getAllTravels();
			session.setAttribute("travels", travels);

			// 24.放入旅行轮播图
			List<Yxfw_travel_rotate> travelRs = travelRotateService.selectAllTravelRotate();
			session.setAttribute("travelRs", travelRs);

			// 25.放入优选旅行
			List<Yxfw_wine> wines = wineService.getAllWines();
			session.setAttribute("wines", wines);

			// 26.放入旅行轮播图
			List<Yxfw_wine_rotate> wineRs = wineRotateService.selectAllWineRotate();
			session.setAttribute("wineRs", wineRs);

			// 27.放入普通浏览手机号用户
			List<User> users = userService.getAllUsers();
			session.setAttribute("users", users);

			return "admin/admin";
		}

		return "admin/error";
	}

	// 查询所有管理员信息
	@RequestMapping("/AllAdmins.do")
	public String allAdminList(Model model) {
		List<Admin> admins = adminService.getAllAdmins(null);
		model.addAttribute("admins", admins);
		// 2放入首页轮播图
		List<Rotate> rotates = rotateService.getAllRotats();
		model.addAttribute("rotates", rotates);

		// 3放入优选婚庆
		List<Yxhq> allYxhqs = yxhqService.getAllYxhq();
		model.addAttribute("allYxhqs", allYxhqs);

		// 4放入优选婚庆工作集
		List<Yxhq_work_picture> yxhqWPs = yxhqWorkPictureService.getAllYxhqWorkPicture();
		model.addAttribute("yxhqWPs", yxhqWPs);

		// 5.放入广告图
		List<Advertisement_picture> advers = advertisementSservice.selectAllAdvers();
		model.addAttribute("advers", advers);

		// 6.放入优选酒店
		List<Yxjd> yxjds = yxjdService.selectAllYxjd();
		model.addAttribute("yxjds", yxjds);

		// 7.放入酒店的轮播图
		List<Yxjd_rotate> yxjdRs = yxjdRotateService.selectAllYxjdRotate();
		model.addAttribute("yxjdRs", yxjdRs);

		// 8.放入主持人
		List<Yxzc> yxzcs = yxzcService.selcectAllYxzc();
		model.addAttribute("yxzcs", yxzcs);

		// 9.放入主持人作品集
		List<Yxzc_work_picture> yxzcWPs = yxzcWorkPictureService.getAllYxhqWorkPicture();
		model.addAttribute("yxzcWPs", yxzcWPs);

		// 10.放入优选摄像
		List<Yxsx> yxsxs = yxsxService.getAllYxhqWorkPicture();
		model.addAttribute("yxsxs", yxsxs);

		// 11.放入优选化妆
		List<Yxhz> yxhzs = yxhzService.getAllYxhz();
		model.addAttribute("yxhzs", yxhzs);

		// 12.放入化妆师作品集
		List<Yxhz_work_picture> yxhzWPs = yxhzWorkPictureService.getAllYxhzWorkPicture();
		model.addAttribute("yxhzWPs", yxhzWPs);

		// 13.放入优选化妆
		List<Yxsy> yxsys = yxsyService.getAllYxhz();
		model.addAttribute("yxsys", yxsys);

		// 14.放入化妆师作品集
		List<Yxsy_work_picture> yxsyWPs = yxsyWorkPictureService.getAllYxsyWorkPicture();
		model.addAttribute("yxsyWPs", yxsyWPs);

		// 15.放入优选化妆
		List<Yxfw_candy> candys = candyService.getAllCandys();
		model.addAttribute("candys", candys);

		// 16.放入酒店的轮播图
		List<Yxfw_candy_rotate> candyRs = candyRotateService.selectAllCandyRotate();
		model.addAttribute("candyRs", candyRs);

		// 17.放入优选化妆
		List<Yxfw_car> cars = carService.getAllCandys();
		model.addAttribute("cars", cars);

		// 18.放入酒店的轮播图
		List<Yxfw_car_rotate> carRs = carRotateService.selectAllCarRotate();
		model.addAttribute("carRs", carRs);

		// 19.放入优选化妆
		List<Yxfw_man> mans = manService.getAllCandys();
		model.addAttribute("mans", mans);

		// 20.放入酒店的轮播图
		List<Yxfw_man_rotate> manRs = manRotateService.selectAllCarRotate();
		model.addAttribute("manRs", manRs);

		// 21.放入优选珠宝
		List<Yxfw_jewellery> jewes = jewelleryService.getAllCandys();
		model.addAttribute("jewes", jewes);

		// 22.放入珠宝轮播图
		List<Yxfw_jewellery_rotate> jeweRs = jewelleryRotateService.selectAllJeweRotate();
		model.addAttribute("jeweRs", jeweRs);

		// 23.放入优选珠宝
		List<Yxfw_travel> travels = travelService.getAllTravels();
		model.addAttribute("travels", travels);

		// 24.放入旅行轮播图
		List<Yxfw_travel_rotate> travelRs = travelRotateService.selectAllTravelRotate();
		model.addAttribute("travelRs", travelRs);

		// 25.放入优选旅行
		List<Yxfw_wine> wines = wineService.getAllWines();
		model.addAttribute("wines", wines);

		// 26.放入旅行轮播图
		List<Yxfw_wine_rotate> wineRs = wineRotateService.selectAllWineRotate();
		model.addAttribute("wineRs", wineRs);

		// 27.放入普通浏览手机号用户
		List<User> users = userService.getAllUsers();
		model.addAttribute("users", users);

		return "admin/admin";
	}

	// 删除管理员,根据ID删除。后面传了一个要删除的id值，比如要删除id是30的用户，整体路径是/delete.do/30
	// id来源于页面上传递时，加上id ？id = ${集合.id}
	@GetMapping("/delete.do")
	public String delete(HttpSession session, Model model, HttpServletRequest request) {
		int id = Integer.parseInt(request.getParameter("id"));

		int num = adminService.deleteAdmin(id);
		// System.out.println(num);返回值为-2147482646 why?
		session.setAttribute("num", num);

		return "redirect:/admin/AllAdmins.do";
	}

	// 添加管理员
	// @PostMapping("/insert.do")
	@RequestMapping(value = "/insert.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> insert(Admin admin, HttpSession session, HttpServletRequest request) {
		HashMap<String, Object> map = new HashMap<String, Object>();

		int num2 = adminService.insertAdmins(admin);

		List<Admin> admins = adminService.getAllAdmins(null);
		map.put("admins", admins);

		if (num2 != 0) {
			map.put("msg", "添加成功");
			System.out.println(map.get("msg"));
			return map;
		} else {
			map.put("msg", "添加失败");
			return map;
		}

	}

	// 修改管理员信息
	@RequestMapping("/update.do")
	public String update(@Param("id") Integer id, @Param("username") String username,
			@Param("password") String password, HttpServletRequest request, Admin admin, Model model) {

		admin.setId(id);
		admin.setUsername(username);
		admin.setPassword(password);
		int num3 = adminService.updateAdmin(admin);
		System.out.println(num3);
		return "redirect:/admin/AllAdmins.do";

	}

	// 跳转到修改管理员的页面

	@RequestMapping("/updateJsp.do")
	public Object updateJsp(Model model, @Param("id") Integer id) {

		// 跳转之前先根据id查询出来
		Admin admins = adminService.selectAdminById(id);
		model.addAttribute("admin", admins);
		return "admin/update";
	}

}
